Defining Time Complexity: The Big Notation
Algorithm Analysis quantifies the resource requirements (time and space) of an algorithm relative to the size of the input ().
We use Big Notation () to describe the worst-case rate of growth (upper bound) of an algorithm's running time as .
Key Principles of Big Analysis:- Focus on Dominant Terms: Drop lower-order terms (e.g., becomes ).
- Ignore Constants: Drop multiplicative constants (e.g., becomes ).
- Worst Case: We generally calculate the performance based on the scenario that maximizes operations.
Big Calculation Cheat Sheet
- Check: If the code execution time does not depend on the input size (e.g., arithmetic, assignment, array lookup), it is .
- Loop Rule (Multiplication): If Loop A () contains Loop B (), the complexity is . If is a constant, the complexity remains .
- Sequential Rule (Addition): If operation A () is followed by operation B (), the complexity is .
- Logarithmic Hint: Any algorithm that repeatedly halves the search space (e.g., dividing by 2 each step) is .
Standard Time Complexity Classes
Understanding these fundamental growth rates is crucial for efficient system design. Note the difference in scale!
| Notation | Name | Growth Rate (N=1 Million) | Example Operation |
|---|---|---|---|
| Constant | Instantaneous | Array Index Access (arr[i]) | |
| Logarithmic | Very Fast (20 Ops) | Binary Search | |
| Linear | 1 Million Ops | Single Loop Iteration | |
| Log-Linear | ~20 Million Ops | Efficient Sorting (Merge Sort) | |
| Quadratic | 1 Trillion Ops | Nested Loops (Brute Force) |
1. Analyze the following Python snippet. Assume `N` is the length of the list `data`.
def example_function(data):
N = len(data)
total = 0
# Outer Loop: Runs N times
for i in range(N):
# Inner Loop: Runs a constant 5 times
for j in range(5):
total += data[i]
What is the Big time complexity of `example_function`?
2. An algorithm has two sequential parts: one is and the other is . What is the overall time complexity?
3. Which of the following complexity classes represents the fastest growth rate (i.e., is the least efficient for large inputs)?
4. Analyze the following Python snippet.
def another_function(data):
N = len(data)
# Loop 1
for i in range(N):
print(i)
# Loop 2
for j in range(N):
print(j)
What is the Big time complexity of `another_function`?